/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hcmus.fit.master.sevensea.core;

import hcmus.fit.master.sevensea.background.Sea;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;

/**
 * 
 * @author hvp
 */
public abstract class AbstractSprite implements Sprite {
    public static List<List<Bitmap>> resources;

    protected Point realPos;
    protected List<Bitmap> frames;
    protected int index;
    protected int nbOfFrame;
    protected Paint paint;
    protected Sea seaMap;
    public int X;
    public int Y;

    public AbstractSprite(Sea seaMap, int x, int y) {
        this.seaMap = seaMap;
        X = x;
        Y = y;
        realPos = translateToView(x, y);
        index = 0;
        paint = new Paint();
    }

    public AbstractSprite(AbstractSprite uc) {
        setRealPos(uc.getRealPos());
        setFrames(uc.getFrames());
        setIndex(uc.getIndex());
        X = uc.X;
        Y = uc.Y;
        setNbOfFrame(uc.getNbOfFrame());
    }

    public Sea getSeaMap() {
        return seaMap;
    }

    public void setSeaMap(Sea seaMap) {
        this.seaMap = seaMap;
    }

    public void moveImmediate(int x, int y) {
        X = x;
        Y = y;
        this.realPos = translateToView(x, y);
    }

    @Override
    public void draw(Canvas canvas) {
        int w = frames.get(index).getWidth() - 1;
        int h = frames.get(index).getHeight() - 1;

        canvas.drawBitmap(frames.get(index), new Rect(0, 0, w, h), new Rect(getRealPos().x
                + Constant.TotalLeft, getRealPos().y + Constant.TotalTop, getRealPos().x
                + Constant.TotalLeft + w, getRealPos().y + Constant.TotalTop + h), this.paint);
    }

    @Override
    public boolean update() {
        index = (index + 1) % nbOfFrame;
        return true;
    }

    public List<Bitmap> getFrames() {
        return frames;
    }

    public void setFrames(List<Bitmap> frames) {
        this.frames = frames;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getNbOfFrame() {
        return nbOfFrame;
    }

    public void setNbOfFrame(int nbOfFrame) {
        this.nbOfFrame = nbOfFrame;
    }

    public Point getRealPos() {
        return realPos;
    }

    public void setRealPos(Point realPos) {
        this.realPos = realPos;
    }

    /**
     * Translate sprite position from MAP to view
     * 
     * @param p
     * @return
     */
    public Point translateToView(android.graphics.Point p) {
        return translateToView(p.x, p.y);
    }

    /**
     * Translate sprite position from MAP to view
     * 
     * @param p
     * @return
     */
    public Point translateToView(int x, int y) {
        return new Point(x * Constant.CELL_WIDTH, y * Constant.CELL_HEIGHT);
    }

    public boolean isShip() {
        return false;
    }

    public boolean isIsland() {
        return false;
    }

    public boolean isWhirl() {
        return false;
    }

    public boolean isRubble() {
        return false;
    }

    public boolean isEnemy() {
        return false;
    }
}
